#include <QDebug>

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "dlg/cvlogindialog.h"
#include "bizlogic/cvactivitymanager.h"
#include "widgets/cvtabitem.h"
#include "bizlogic/microblogmanager.h"
#include "widgets/tasktabitemwidget.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    CVSession *session = CVSession::get();
    CVUser *user = session->getUser();
    ui->UserName->setText(user->getName());
    //connect(ui->UserName, SIGNAL(linkActivated(QString)), this, SLOT(logoutAction(QString)));
    ui->LoggedUserIcon->setImageUrl(user->getIcon());


    item = new ActivityTabItemWidget();
    ui->tabWidget->addTab(item, "My Activity");
    item->loadActivities();
    /*
    TaskTabItemWidget *item1 = new TaskTabItemWidget();
    ui->tabWidget->addTab(item1, "Action Items");
    item1->load();
    */
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initStatusBar()
{

}

void MainWindow::handleData(QList<CVActivity*> list, const QString &message)
{
    qDebug() << message;
}

void MainWindow::createMicroblog()
{
    QString status = ui->UserStatus->text();
    if (!status.trimmed().isEmpty())
    {
        MicroBlogManager *mbMgr = new MicroBlogManager();
        mbMgr->createMicroblog(status);
        connect(mbMgr, SIGNAL(handleData(bool)), item, SLOT(reload(bool)));
    }
    this->setFocus();
}

void MainWindow::logoutAction()
{
    this->hide();

    CVImageCache *cache = CVImageCache::instance();
    cache->clear();
    CVSession *session = CVSession::get();
    session->clear();

    CVLoginDialog *dlg = new CVLoginDialog();
    dlg->show();
}

void MainWindow::logoutAction(const QString &str)
{
    qDebug() << "MainWindow::logoutAction" << str;
}
